<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 工具类库
 */
class Utility {

    /**
     * 加密方法
     *
     * @param string $str
     * @param string $key
     * @return string
     */
    public static function encode($str, $key = '') {
        $CI =& get_instance();
        $CI->load->library('encrypt');
        return $CI->encrypt->encode($str, $key);
    }

    /**
     * 解密方法
     *
     * @param string $str
     * @param string $key
     * @return string
     */
    public static function decode($str, $key = '') {
        $CI =& get_instance();
        $CI->load->library('encrypt');
        return $CI->encrypt->decode($str, $key);
    }

    /**
     * 密码加密存储
     *
     * @param string $pwd 密码
     * @param string $salt 盐值
     * @return string
     */
    public static function get_pwd_code($pwd, $salt = '') {
        $CI =& get_instance();
        $salt || $salt = $CI->config->item('encryption_key');
        return md5($pwd) . $salt;
    }

    /**
     * 字符串长度
     *
     * @param string $str
     * @return string
     */
    public static function strlen($str) {
        $CI =& get_instance();
        $CI->load->helper('string');
        return my_strlen($str, $CI->config->item('charset'));
    }

    /**
     * 字符串截取
     *
     * @param string $str
     * @param int $length
     * @param int $start
     * @param bool $dot
     * @return string
     */
    public static function substrs($str, $length, $start = 0, $dot = true) {
        if (self::strlen($str) <= $length) return $str;
        $CI =& get_instance();
        $CI->load->helper('string');
        return my_substr($str, $start, $length, $CI->config->item('charset'), $dot);
    }

    /**
     * 页码转sql
     *
     * @param int $page 页数
     * @param int $perpage 每页显示数
     * @return array <1.start 2.limit>
     */
    public static function page2limit($page, $perpage = 10) {
        $limit = intval($perpage);
        $start = max(($page - 1) * $limit, 0);
        return array($start, $limit);
    }

    /**
     * 将时间字串转化成零时区时间戳返回
     *
     * @param string $str 格式良好的时间串
     * @return int
     */
    public static function strtotime($str) {
        return strtotime($str);
    }

    /**
     * 时间戳转字符串
     *
     * @example Y-m-d H:i:s  2012-12-12 12:12:12
     * @param int $timestamp 时间戳
     * @param string $format 时间格式
     * @return string
     */
    public static function time2str($timestamp, $format = 'Y-m-d H:i') {
        if (!$timestamp) return '';
        if ($format == 'auto') return self::_time2cpstr($timestamp);
        return gmdate($format, $timestamp);
    }

    protected static function _time2cpstr($timestamp) {
        $current = self::getTime();
        $decrease = $current - $timestamp;
        if ($decrease < 0) return self::time2str($timestamp);
        if ($decrease < 60) return $decrease . '秒前';
        if ($decrease < 3600) return ceil($decrease / 60) . '分钟前';
        $decrease = self::str2time(self::time2str($current, 'Y-m-d')) - self::str2time(self::time2str($timestamp, 'Y-m-d'));
        if ($decrease == 0) return self::time2str($timestamp, 'H:i');
        if ($decrease == 86400) return '昨天' . self::time2str($timestamp, 'H:i');
        if ($decrease == 172800) return '前天' . self::time2str($timestamp, 'H:i');
        if (self::time2str($timestamp, 'Y') == self::time2str($current, 'Y')) return self::time2str($timestamp, 'm-d H:i');
        return self::time2str($timestamp);
    }

    /**
     * 获取今日零点时间戳
     *
     * @return int
     */
    public static function get_tody_time() {
        return self::str2time(self::time2str(TIMESTAMP, 'Y-m-d'));
    }

    /**
     * 获取图片路径
     *
     * @param string $path
     * @param int $ifthumb 0:没有缩略图/1：缩略图/2:迷你缩略图
     * @param bool $isLocal 是否强制使用本地存储 (默认自动选择)
     * @return string
     */
    public static function get_path($path, $ifthumb = 0, $isLocal = false) {

    }

    /**
     * 获取用户头像地址
     *
     * @param int $uid
     * @param string $size <m.中头像 s.小头像>
     * @return string
     */
    public static function get_avatar($uid, $size = 'middle') {
        $file = $uid . (in_array($size, array('middle', 'small')) ? '_' . $size : '') . '.jpg';
        return '/uploads/avatar/' . self::get_user_dir($uid) . '/' . $file;
    }

    /**
     * 获取用户头像存储目录
     *
     * @param int $uid
     * @return string
     */
    public static function get_user_dir($uid) {
        $uid = sprintf("%09d", $uid);
        return substr($uid, 0, 3) . '/' . substr($uid, 3, 2) . '/' . substr($uid, 5, 2);
    }

    /**
     * 删除附件
     *
     * @param string $path 附件相对地址
     * @param int $ifthumb 缩略图
     * @param bool $isLocal 是否强制使用本地存储 (默认自动选择)
     * @return bool
     */
    public static function delete_attach($path, $ifthumb = 0, $isLocal = false) {

    }

    /**
     * 删除本地文件
     *
     * @param string $filename 文件绝对地址
     * @return bool
     */
    public static function delete_file($filename) {

    }

    /**
     * 返回html checked
     *
     * @param boolean $var
     * @return string
     */
    public static function if_check($var) {
        return $var ? ' checked' : '';
    }

    /**
     * 返回html selected
     *
     * @param boolean $var
     * @return string
     */
    public static function if_select($var) {
        return $var ? ' selected' : '';
    }

    /**
     * 返回html current
     *
     * @param boolean $var
     * @return string
     */
    public static function if_current($var) {
        return $var ? ' current' : '';
    }

    /**
     * 检查是否在线
     *
     * @param int $time lastvisit
     * @return bool
     */
    public static function if_online($time) {
        $onlinetime = 30;
        if ($time + $onlinetime * 60 > TIMESTAMP) return true;
        return false;
    }

    /**
     * 位运算比对
     *
     * @param int $status 状态码
     * @param int $b 比对位置
     * @param int $len 比对位数
     * @return int
     */
    public static function get_status($status, $b, $len = 1) {
        return $status >> --$b & (1 << $len) - 1;
    }
}

/* End of file Utility.php */
/* Location: ./application/libraries/Utility.php */
